class AppState: ObservableObject {
    @Published var dataFromAnotherWindow: String = ""
}

struct ContentView: View {
    @EnvironmentObject var appState:AppState
    
    var body: some View {
        Text(appState.dataFromAnotherWindow)
            .onTapGesture{
                // 切换至另一视图/窗口，并允许其修改 `appState` 内的数据
                AnotherView().environmentObject(self.appState)
            }
    }
}

struct FvtToolbar: ViewModifier {
    var title: String 
    var rightTitle: String 
    var rightAction: ()->()

    init(_ title:String, _ rightTitle:String, _ rightAction:@escaping ()->()){
        self.title = title 
        self.rightTitle = rightTitle 
        self.rightAction = rightAction 
    }

    func body(content: Content) -> some View {
        content
            .toolbar {
                ToolbarItem(placement:.principal){
                    Text(title).font(.headline)
                }
                ToolbarItem(placement:.navigationBarTrailing){
                    Button(action:rightAction,label:{Text(rightTitle)})
                }
            }
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(AppState())
        }
    }
}